package win.timloong.android.carwiki.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

import win.timloong.android.carwiki.R;
import win.timloong.android.carwiki.bean.IndexItem;
import win.timloong.android.carwiki.util.ImageUtil;

/**
 * Created by tim on 16/7/12.
 */

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.Holder> {
    private ArrayList<IndexItem> arrayList = new ArrayList<>();

    public ItemAdapter(ArrayList<IndexItem> arrayList) {
        this.arrayList = arrayList;
    }

    public void addData(ArrayList<IndexItem> arrayList) {
        this.arrayList.addAll(arrayList);

    }

    public void addData(IndexItem indexItem) {
        this.arrayList.add(indexItem);
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.index_item, parent,false));
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        IndexItem indexItem = arrayList.get(position);
        ImageUtil.loadImage(indexItem.getImage(), holder.imageView);
        holder.title.setText(indexItem.getTitle() != null ? indexItem.getTitle() : "");
        ImageUtil.loadImage(indexItem.getTypeImg() == 0 ? R.mipmap.type_jinghua : R.mipmap.type_tashuo, holder.typeImg);
        holder.type.setText(indexItem.getTypeName() != null ? indexItem.getTypeName() : "");
        holder.hasSeen.setText(indexItem.getHasSeen() != null ? indexItem.getHasSeen() : "0");
        holder.time.setText(indexItem.getTime() != null ? indexItem.getTime() : "");
    }


    @Override
    public int getItemCount() {
        return arrayList.size();
    }


    class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView title;
        ImageView typeImg;
        TextView type;
        TextView hasSeen;
        TextView time;

        public Holder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.index_item_image);
            title = (TextView) itemView.findViewById(R.id.index_item_title);
            typeImg = (ImageView) itemView.findViewById(R.id.index_item_typeimage);
            type = (TextView) itemView.findViewById(R.id.index_item_typename);
            hasSeen = (TextView) itemView.findViewById(R.id.index_item_hasseen);
            time = (TextView) itemView.findViewById(R.id.index_item_time);
        }
    }
}
